<style src="../../style/statistics.css" scoped></style>
<template>
  <div class="containerTable">
    <!--表5蔬菜生产经营实体表T_VegProsecution-->
    <Row class="divSearch">
            <Col span="24" class="col-margin">
            <!--根据需求删除不不要查询条件-->
            所属表：
            <Select v-model="searchModel.TableCode" class="searchInput"  placeholder="全部" @on-change="getPageData()">
                <Option :value="item.DictValue" v-for="item in tableList" :key="item.DictValue">{{item.DictText}}({{item.DictValue}})</Option>
            </Select>
            所属父级：
            <Select v-model="searchModel.ParentCode"  class="searchInput" placeholder="全部" filterable @on-change="getPageData()">
                <Option value="">全部</Option>
                <Option v-for="option in searchModel.options" :value="option.ItemCode" :key="option" :label="option.ItemName" >
                    <span>
                        <b>{{option.ItemName}}</b>
                    </span>
                    <br/>
                    <span style="color:#8d8b8b">代码:{{option.ItemCode}}</span>
                    <br/>
                    <span style="color:#8d8b8b">父级代码:{{option.ParentCode}}</span>
                </Option>
            </Select>
            状态
            <Select v-model="searchModel.Status" class="searchInput" placeholder="全部" @on-change="getPageData()">
                <Option value="">全部</Option>
                <Option value="1">启用</Option>
                <Option value="0">停用</Option>
            </Select>
            代码：
            <Input v-model="searchModel.ItemCode" placeholder="请输入查询的代码..." class="searchInput"></Input>
            名称：
            <Input v-model="searchModel.ItemName" placeholder="请输入查询的名称..." class="searchInput"></Input>
            <Button icon="ios-search" type="ghost" @click="getPageData()">查询</Button>
            <Button icon="plus" type="primary" @click="showType = true"  class='searchButtonRight'>新增</Button>
            </Col>
        </Row>
        <Row>
            <Col span="24" class="col-margin">
            <Table size="small" @on-select="selectSingel" @on-select-all="selectAll" :height="clientHeight" border :context="ctx" :columns="columnsList" :data="datas.rows" no-data-text='暂无数据...'></Table>
            </Col>
        </Row>
        <Row>
            <Col span="24">
            <Page class="page-style" :total="datas.total" placement="top" @on-change="getPageNumber" :page-size="searchModel.PageSize" :page-size-opts='pageSizeOpts' show-sizer @on-page-size-change="getPageSize" show-total show-elevator></Page>
            </Col>
        </Row>
        <Modal v-model="showType" :mask-closable="false" :closable="false">
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">信息编辑</span>
            </p>
            <div slot="footer">
                <Button type="primary" @click="save('dataModel')">提交</Button>
                <Button type="ghost" @click="cancel('dataModel')" style="margin-left: 8px">取消</Button>
            </div>
            <Form ref="dataModel" :model="dataModel" :rules="ruleValidate" :label-width="100">
                <Form-item style="display:none;" prop="Id">
                    <Input v-model="dataModel.Id"></Input>
                </Form-item>
                <Form-item label="所属表" prop="TableCode">
                    <Select v-model="dataModel.TableCode" placeholder="请选择所属表" @on-change="getEditItemList">
                        <Option :value="item.DictValue" v-for="item in tableList" :key="item.DictValue">{{item.DictText}}({{item.DictValue}})</Option>
                    </Select>
                </Form-item>
                <Form-item label="所属父级分类" prop="ParentCode">
                    <Select v-model="dataModel.ParentCode" placeholder="无父级分类" filterable>
                        <Option value="">无父级分类</Option>
                        <Option v-for="option in editOptionList" :value="option.ItemCode" :key="option" :label="option.ItemName">
                            <span>
                                <b>{{option.ItemName}}</b>
                            </span>
                            <br/>
                            <span style="color:#8d8b8b">代码:{{option.ItemCode}}</span>
                            <br/>
                            <span style="color:#8d8b8b">父级代码:{{option.ParentCode}}</span>
                        </Option>
                    </Select>
                </Form-item>
                <Form-item label="分类名称" prop="ItemName">
                    <Input v-model="dataModel.ItemName" placeholder="请输入分类名称"></Input>
                </Form-item>
                <Form-item label="代码" prop="ItemCode">
                    <Input v-model="dataModel.ItemCode" placeholder="请输入代码"></Input>
                </Form-item>
                <Form-item label="计量单位" prop="UnitName">
                    <Input v-model="dataModel.UnitName" placeholder="请输入计量单位"></Input>
                </Form-item>
                <Form-item label="状态" prop="Status">
                    <Select v-model="dataModel.Status" placeholder="请选择状态">
                        <Option value="1">启用</Option>
                        <Option value="0">停用</Option>
                    </Select>
                </Form-item>
                <Form-item label="排序" prop="Sort">
                    <Input v-model="dataModel.Sort" placeholder="请输入排序"></Input>
                </Form-item>
                <Form-item label="备注" prop="Remark">
                    <Input v-model="dataModel.Remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注长度请小于150个字符..."></Input>
                </Form-item>
            </Form>
        </Modal>
    </div>
</template>
<script>
export default {
    data() {
        return {
            SetList: [],//级联选择列表
            tableList: this.GetGroupDict("ReportTableName"),//用户类型
            showType: false,//编辑框弹出状态
            ruleValidate: {//验证模型
                ParentCode: [{ required: false, message: '父级项目编号不能为空', trigger: 'blur' }],
                ItemName: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
                ItemCode: [{ required: true, message: '代码不能为空', trigger: 'blur' }],
                UnitName: [{ required: false, message: '计量单位不能为空', trigger: 'blur' }],
                Status: [{ required: false, message: '状态不能为空', trigger: 'blur' }],
                Sort: [{ required: false, message: '排序不能为空', trigger: 'blur' }],
                Remark: [
                    { required: false, message: '备注长度请小于100个字符', trigger: 'blur' },
                    { type: 'string', max: 100, message: '备注长度请小于150个字符', trigger: 'blur' }
                ],
                TableCode: [{ required: true, message: '所属表不能为空', trigger: 'blur' }],
            },
            dataModel: {//编辑模型
                Id: 0,
                ParentCode: '',
                ItemName: '',
                ItemCode: '',
                UnitName: '',
                Status: '1',
                Sort: '1',
                Remark: '',
                TableCode: '',
                loading: false,
                SetIds: []
            },
            editOptionList: [],
            clientHeight: window.innerHeight - 260,//表格默认高度
            //查询模型：根据需求删除不不要查询条件
            searchModel: {
                Id: '',
                ParentCode: '',
                ItemName: '',
                ItemCode: '',
                UnitName: '',
                Status: '',
                Sort: '',
                Remark: '',
                TableCode: '',
                loading: false,
                options: [],
                PageNumber: 1, PageSize: 20
            },
            pageSizeOpts: [20, 40, 50],//默认分页最大设置
            datas: {},//table数据对象
            dataIds: [],
            ctx: this,//table数据上下文
            columnsList: [//表头对象
                // { type: 'selection', width: 60, align: 'center' },//多选设置
                { title: "父级项目编号", key: "ParentCode", width: 120 },
                { title: "分类名称", key: "ItemName", width: 200 },
                { title: "代码", key: "ItemCode", width: 150 },
                { title: "计量单位", key: "UnitName" , width: 90 },
                {
                     title: "状态", width: 90 , key: "Status", render:(h,params) => {                         
                        if (params.row.Status === "1") {
                            return "启用";
                        } else {
                            return "停用";
                        }
                    }
                },
                { title: "所属表", key: "TableCode", width: 180 },
                { title: "备注", key: "Remark" },
                {
                    title: "操作", key: "Id", width: 180, align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                class:{
                                    ieditColor:true
                                },
                                props: {
                                    size: 'small',
                                    type:'ghost',
                                    icon: 'ios-compose'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.editInfo(params.row.Id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                class:{
                                    ideleteColor:true
                                },
                                props: {
                                    size: 'small',
                                    type:'ghost',
                                    icon: 'ios-trash-outline'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.delInfo(params.row.Id)
                                    }
                                }
                            }, '删除'),
                        ]);
                    }
                }]
        }
    },
    mounted() {
        //table高度计算方法
        const that = this;
        window.onresize = () => {
            return (() => {
                that.clientHeight = window.innerHeight - 260;
            })()
        };
    },
    methods: {
        getSearchItemList() {
            if (this.searchModel.TableCode != null && this.searchModel.TableCode != '') {
                var self = this;
                this.$http.get('SetList/GetSearchList', { params: { tableCode: this.searchModel.TableCode } }).then(function (res) {
                    self.searchModel.options = res.data.model;
                });
            }
        },
        getEditItemList(tablecode) {
            var self = this;
            this.$http.get('SetList/GetSearchList', { params: { tableCode: tablecode } }).then(function (res) {
                self.editOptionList = res.data.model;
            });
        },
        //获取当前页码
        getPageNumber(pageIndex) {
            this.searchModel.PageNumber = pageIndex;
            this.getPageData();
        },
        //获取当前分页大小
        getPageSize(pageSize) {
            this.searchModel.PageSize = pageSize;
            this.getPageData();
        },
        //获取this.datas: {},//table数据对象
        getPageData() {
            this.getSearchItemList();
            var self = this;
            self.$http.post('SetList/PostPageList', this.searchModel).then(function (res) {
                if (res.data.status === 1) {
                    self.datas = res.data;
                    console.log('3377')
                    console.log(self.datas)
                }
                else {
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },
        //编辑获取对象传入id
        editInfo(id) {//编辑
            var self = this;
            this.$http.get("SetList", { params: { id: id } }).then(//获取数据编辑项的值
                function (res) {
                    if (res.data.status == 1) {
                        self.getEditItemList(res.data.model.TableCode);
                        self.dataModel = res.data.model;
                        self.modalShow();//打开modal
                        // setTimeout(() => {

                        // }, 200);
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "编辑数据加载失败!!!" });
                    }
                }
            );
        },
        //删除对象
        delInfo(id) {
            var self = this;
            self.$Modal.confirm({
                title: '提示信息',
                content: '<p>数据删除后不可恢复，确定要删除？</p>',
                onOk: () => {
                    this.$http.delete("SetList", { params: { id: id } }).then(
                        function (res) {
                            if (res.data.status == 1) {
                                self.$Notice.success({ title: '删除成功！', desc: false });
                                self.getPageData();
                            }
                            else {
                                self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                            }
                        }
                    );
                }
            });
        },
        //保存方法
        save(modelname) {
            var self = this;
            this.$refs[modelname].validate((valid) => {
                if (valid) {
                    if (this.dataModel.Id === 0) {
                        this.$http.post('SetList', this.dataModel).then(
                            function (res) {
                                if (res.data.status == 1) {
                                    self.getPageData();//读取分页数据
                                    self.restModel(modelname);//重置model
                                    self.modalHide();//关闭modal
                                    self.$Notice.success({ title: '保存成功！', desc: false });
                                }
                                else {
                                    self.$Modal.warning({ title: "提示信息", content: "添加失败,请与管理员联系!" });
                                }
                            });
                    } else {
                        this.$http.put('SetList', this.dataModel).then(
                            function (res) {
                                if (res.data.status == 1) {
                                    self.getPageData();//读取分页数据
                                    self.restModel(modelname);//重置model
                                    self.modalHide();//关闭modal
                                    self.$Notice.success({ title: '更新成功！', desc: false });
                                }
                                else {
                                    self.$Modal.warning({ title: "提示信息", content: "更新失败,请与管理员联系!" });
                                }
                            });
                    }
                }
            });
        },
        //编辑框打开
        modalShow() {
            this.showType = true;
        },
        //编辑框关闭
        modalHide() {
            this.showType = false;
        },
        //取消方法
        cancel(modelname) {
            this.$refs[modelname].resetFields();//重置model
            this.showType = false;//关闭modal
        },
        //重置model
        restModel(modelname) {
            this.$refs[modelname].resetFields();//重置model
        },
        //全选方法
        selectAll(selection) {
            this.dataIds = selection;
        },
        //checkbox选中方法
        selectSingel(selection, row) {
            this.dataIds = selection;
        },
        //checkbox取消选中方法
        selecChange(selection) {
            this.dataIds = selection;
        },
        //批量删除
        multiSelect() {
            var self = this;
            if (this.dataIds.length !== 0) {
                self.$Modal.confirm({
                    title: '提示信息',
                    content: '<p>数据删除后不可恢复，确定要删除？</p>',
                    onOk: () => {
                        var ids = this.getDataIds(this.dataIds);
                        this.$http.post("SetList/PostBatchDelete", ids).then(
                            function (res) {
                                if (res.data.status == 1) {
                                    self.dataIds = [];
                                    self.$Notice.success({ title: '删除成功！', desc: false });
                                    self.getPageData();
                                }
                                else {
                                    self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                                }
                            }
                        );
                    }
                });
            } else {
                self.$Modal.warning({ title: "提示信息", content: "请选择需要删除的数据!" });
            }

        },
        getDataIds(idList) {
            var ids = [];
            if (idList.length !== 0) {
                for (var i = 0; i < idList.length; i++) {
                    ids.push(idList[i].Id);
                }
            }
            return ids;
        }
    },
    //创建Vue页面方法
    created: function () {
        //创业Vue前调用获取this.datas: {},//table数据对象
        this.getPageData();
    },
    watch: {
        //需要监听的在此处新增监听对象
    }
}
</script>
